package com.栈;

import java.util.Scanner;
import java.util.Stack;

/**
 * 栈
 */
public class MyStack {
    public static void main(String[] args){

        Stack<Integer> stack = new Stack<>();


        int  i;
        while (true){
            // System.out.println("出队输入0");
            //System.out.println("结束输入-1");
            //-2  查询栈头
            System.out.println("输入入栈数字：");
            i = new Scanner(System.in).nextInt();
            if(i == -2){
                if(!stack.isEmpty()){
                    System.out.println("栈头:"+stack.peek());

                }
                else{
                    System.out.println("空栈");
                }
                continue;
            }
            if(i == -1){
                System.out.println("结束栈");
                break;
            }
            if(i==0){
                if(stack.isEmpty()){
                    System.out.println("空栈");
                    continue;
                }
                stack.pop();
                System.out.println(stack.toString());
            }
            else{
               stack.push(i);
                System.out.println(stack.toString());

            }

        }
    }
}
